﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Smartdispatch
{
    public partial class Settings : Form
    {
        private string ConnectStr = $@"Data Source=192.168.0.89;Initial Catalog=Runtime;User ID=sa;Password=abc123!@#";
        public Settings()
        {
            InitializeComponent();
            var ima = Image.FromFile("backgroup.gif");

            this.BackgroundImage = ima;
            this.BackgroundImageLayout = ImageLayout.Stretch;
            this.Load += Settings_Load;
            Timer timer = new Timer();
            timer.Interval = 1000 * 60 * 30;
            timer.Tick += Timer_Tick;
            timer.Enabled = true;
            timer.Start();
            Timer_Tick(timer, new EventArgs());
            this.OnLoad(new EventArgs());
        }
        /// <summary>
        /// 更新目标液位
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Timer_Tick(object sender, EventArgs e)
        {
            int hour = DateTime.Now.Hour;
            if (hour>22 || hour<6)
            {
                textBox19.Text = textBox2.Text;
                textBox20.Text = textBox1.Text;
                textBox21.Text = textBox11.Text;
                textBox22.Text = textBox12.Text;
                textBox23.Text = textBox17.Text;
                textBox24.Text = textBox18.Text;

            }
            else if (hour>7 && hour<17)
            {
                textBox19.Text = textBox4.Text;
                textBox20.Text = textBox3.Text;
                textBox21.Text = textBox9.Text;
                textBox22.Text = textBox10.Text;
                textBox23.Text = textBox15.Text;
                textBox24.Text = textBox16.Text;
            }
            else if (hour>=17&& hour<21)
            {
                textBox19.Text = textBox6.Text;
                textBox20.Text = textBox5.Text;
                textBox21.Text = textBox7.Text;
                textBox22.Text = textBox8.Text;
                textBox23.Text = textBox13.Text;
                textBox24.Text = textBox14.Text;
            }
        }

        /// <summary>
        /// 同步设置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Settings_Load(object sender, EventArgs e)
        {
            string sqlstr = "select Name ,value from K_Dispatchsetings";
            SqlConnection sqlCnt = new SqlConnection(ConnectStr);
            try
            {
                sqlCnt.Open();
                SqlCommand command = new SqlCommand(sqlstr, sqlCnt);
                var result = command.ExecuteReader();
                if (result.HasRows)
                {
                    while (result.Read())
                    {
                        if (result["Name"].ToString() == checkBox1.Name)
                        {
                            checkBox1.Checked = bool.Parse(result["value"].ToString());
                        }
                        else if (result["Name"].ToString() == checkBox2.Name)
                        {
                            checkBox2.Checked = bool.Parse(result["value"].ToString());
                        }
                        else if (result["Name"].ToString() == checkBox3.Name)
                        {
                            checkBox3.Checked = bool.Parse(result["value"].ToString());
                        }
                        else if (result["Name"].ToString() == checkBox4.Name)
                        {
                            checkBox4.Checked = bool.Parse(result["value"].ToString());
                        }
                        else if (result["Name"].ToString() == checkBox5.Name)
                        {
                            checkBox5.Checked = bool.Parse(result["value"].ToString());
                        }
                        else if (result["Name"].ToString() == checkBox6.Name)
                        {
                            checkBox6.Checked = bool.Parse(result["value"].ToString());
                        }
                        else if (result["Name"].ToString() == checkBox7.Name)
                        {
                            checkBox7.Checked = bool.Parse(result["value"].ToString());
                        }
                        else if (result["Name"].ToString() == checkBox8.Name)
                        {
                            checkBox8.Checked = bool.Parse(result["value"].ToString());
                        }
                        else if (result["Name"].ToString() == checkBox9.Name)
                        {
                            checkBox9.Checked = bool.Parse(result["value"].ToString());
                        }
                        else if (result["Name"].ToString() == checkBox10.Name)
                        {
                            checkBox10.Checked = bool.Parse(result["value"].ToString());
                        }
                        else if (result["Name"].ToString() == checkBox11.Name)
                        {
                            checkBox11.Checked = bool.Parse(result["value"].ToString());
                        }
                        else if (result["Name"].ToString() == checkBox12.Name)
                        {
                            checkBox12.Checked = bool.Parse(result["value"].ToString());
                        }
                        else if (result["Name"].ToString() == checkBox13.Name)
                        {
                            checkBox13.Checked = bool.Parse(result["value"].ToString());
                        }
                        else if (result["Name"].ToString() == checkBox14.Name)
                        {
                            checkBox14.Checked = bool.Parse(result["value"].ToString());
                        }
                        else if (result["Name"].ToString() == checkBox15.Name)
                        {
                            checkBox15.Checked = bool.Parse(result["value"].ToString());
                        }
                        else if (result["Name"].ToString() == checkBox16.Name)
                        {
                            checkBox16.Checked = bool.Parse(result["value"].ToString());
                        }
                        else if (result["Name"].ToString() == checkBox17.Name)
                        {
                            checkBox17.Checked = bool.Parse(result["value"].ToString());
                        }
                        else if (result["Name"].ToString() == checkBox18.Name)
                        {
                            checkBox18.Checked = bool.Parse(result["value"].ToString());
                        }
                        else if (result["Name"].ToString() == checkBox19.Name)
                        {
                            checkBox19.Checked = bool.Parse(result["value"].ToString());
                        }
                        else if (result["Name"].ToString() == checkBox20.Name)
                        {
                            checkBox20.Checked = bool.Parse(result["value"].ToString());
                        }
                        else if (result["Name"].ToString() == checkBox22.Name)
                        {
                            checkBox22.Checked = bool.Parse(result["value"].ToString());
                        }
                        else if (result["Name"].ToString() == checkBox21.Name)
                        {
                            checkBox21.Checked = bool.Parse(result["value"].ToString());
                        }
                        else if (result["Name"].ToString() == checkBox23.Name)
                        {
                            checkBox23.Checked = bool.Parse(result["value"].ToString());
                        }
                        else if (result["Name"].ToString() == checkBox24.Name)
                        {
                            checkBox24.Checked = bool.Parse(result["value"].ToString());
                        }
                        else if (result["Name"].ToString() == checkBox25.Name)
                        {
                            checkBox25.Checked = bool.Parse(result["value"].ToString());
                        }
                        else if (result["Name"].ToString() == checkBox26.Name)
                        {
                            checkBox26.Checked = bool.Parse(result["value"].ToString());
                        }
                        else if (result["Name"].ToString() == checkBox27.Name)
                        {
                            checkBox27.Checked = bool.Parse(result["value"].ToString());
                        }
                        else if (result["Name"].ToString() == textBox1.Name)
                        {
                            textBox1.Text = result["value"].ToString();
                        }
                        else if (result["Name"].ToString() == textBox2.Name)
                        {
                            textBox2.Text = result["value"].ToString();
                        }
                        else if (result["Name"].ToString() == textBox3.Name)
                        {
                            textBox3.Text = result["value"].ToString();
                        }
                        else if (result["Name"].ToString() == textBox4.Name)
                        {
                            textBox4.Text = result["value"].ToString();
                        }
                        else if (result["Name"].ToString() == textBox5.Name)
                        {
                            textBox5.Text = result["value"].ToString();
                        }
                        else if (result["Name"].ToString() == textBox6.Name)
                        {
                            textBox6.Text = result["value"].ToString();
                        }
                        else if (result["Name"].ToString() == textBox7.Name)
                        {
                            textBox7.Text = result["value"].ToString();
                        }
                        else if (result["Name"].ToString() == textBox8.Name)
                        {
                            textBox8.Text = result["value"].ToString();
                        }
                        else if (result["Name"].ToString() == textBox9.Name)
                        {
                            textBox9.Text = result["value"].ToString();
                        }
                        else if (result["Name"].ToString() == textBox11.Name)
                        {
                            textBox11.Text = result["value"].ToString();
                        }
                        else if (result["Name"].ToString() == textBox12.Name)
                        {
                            textBox12.Text = result["value"].ToString();
                        }
                        else if (result["Name"].ToString() == textBox13.Name)
                        {
                            textBox13.Text = result["value"].ToString();
                        }
                        else if (result["Name"].ToString() == textBox14.Name)
                        {
                            textBox14.Text = result["value"].ToString();
                        }
                        else if (result["Name"].ToString() == textBox15.Name)
                        {
                            textBox15.Text = result["value"].ToString();
                        }
                        else if (result["Name"].ToString() == textBox16.Name)
                        {
                            textBox16.Text = result["value"].ToString();
                        }
                        else if (result["Name"].ToString() == textBox17.Name)
                        {
                            textBox17.Text = result["value"].ToString();
                        }
                        else if (result["Name"].ToString() == textBox18.Name)
                        {
                            textBox18.Text = result["value"].ToString();
                        }
                        else if (result["Name"].ToString() == textBox10.Name)
                        {
                            textBox10.Text = result["value"].ToString();
                        }
                    }
                }
                sqlCnt.Close();
            }
            catch(Exception ex)
            {
                MessageBox.Show("配置同步失败，原因："+ex.Message);
            }
        }
        /// <summary>
        /// 保存设置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            string sqlstr = "delete from K_Dispatchsetings ";
            try
            {
                SqlConnection sqlCnt = new SqlConnection(ConnectStr);
                sqlCnt.Open();
                SqlCommand command = new SqlCommand(sqlstr, sqlCnt);
                command.ExecuteNonQuery();
                sqlstr = $@"insert into K_Dispatchsetings(Name,value) values
('{checkBox1.Name}','{checkBox1.Checked.ToString()}'),
('{checkBox2.Name}','{checkBox2.Checked.ToString()}'),
('{checkBox3.Name}','{checkBox3.Checked.ToString()}'),
('{checkBox4.Name}','{checkBox4.Checked.ToString()}'),
('{checkBox5.Name}','{checkBox5.Checked.ToString()}'),
('{checkBox6.Name}','{checkBox6.Checked.ToString()}'),
('{checkBox7.Name}','{checkBox7.Checked.ToString()}'),
('{checkBox8.Name}','{checkBox8.Checked.ToString()}'),
('{checkBox9.Name}','{checkBox9.Checked.ToString()}'),
('{checkBox10.Name}','{checkBox10.Checked.ToString()}'),
('{checkBox11.Name}','{checkBox11.Checked.ToString()}'),
('{checkBox13.Name}','{checkBox13.Checked.ToString()}'),
('{checkBox14.Name}','{checkBox14.Checked.ToString()}'),
('{checkBox15.Name}','{checkBox15.Checked.ToString()}'),
('{checkBox16.Name}','{checkBox16.Checked.ToString()}'),
('{checkBox17.Name}','{checkBox17.Checked.ToString()}'),
('{checkBox18.Name}','{checkBox18.Checked.ToString()}'),
('{checkBox19.Name}','{checkBox19.Checked.ToString()}'),
('{checkBox20.Name}','{checkBox20.Checked.ToString()}'),
('{checkBox21.Name}','{checkBox21.Checked.ToString()}'),
('{checkBox22.Name}','{checkBox22.Checked.ToString()}'),
('{checkBox23.Name}','{checkBox23.Checked.ToString()}'),
('{checkBox24.Name}','{checkBox24.Checked.ToString()}'),
('{checkBox25.Name}','{checkBox25.Checked.ToString()}'),
('{checkBox26.Name}','{checkBox26.Checked.ToString()}'),
('{checkBox27.Name}','{checkBox27.Checked.ToString()}'),
('{textBox1.Name}','{textBox1.Text}'),
('{textBox2.Name}','{textBox2.Text}'),
('{textBox3.Name}','{textBox3.Text}'),
('{textBox4.Name}','{textBox4.Text}'),
('{textBox5.Name}','{textBox5.Text}'),
('{textBox6.Name}','{textBox6.Text}'),
('{textBox7.Name}','{textBox7.Text}'),
('{textBox8.Name}','{textBox8.Text}'),
('{textBox9.Name}','{textBox9.Text}'),
('{textBox10.Name}','{textBox10.Text}'),
('{textBox11.Name}','{textBox11.Text}'),
('{textBox12.Name}','{textBox12.Text}'),
('{textBox13.Name}','{textBox13.Text}'),
('{textBox14.Name}','{textBox14.Text}'),
('{textBox15.Name}','{textBox15.Text}'),
('{textBox16.Name}','{textBox16.Text}'),
('{textBox17.Name}','{textBox17.Text}'),
('{textBox18.Name}','{textBox18.Text}')
";
                SqlCommand cmd = new SqlCommand(sqlstr, sqlCnt);
                cmd.ExecuteNonQuery();
                sqlCnt.Close();
            }
            catch(Exception ex)
            {
                MessageBox.Show("保存失败，请检查，原因："+ex.Message);
            }
            this.Visible = false;
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked)
            {
                checkBox20.Enabled = true;
                checkBox21.Enabled = true;
            }
            else
            {
                checkBox20.Enabled = false;
                checkBox21.Enabled = false;
                checkBox20.Checked = false;
                checkBox21.Checked = false;
            }
        }

        private void checkBox2_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox2.Checked)
            {
                checkBox22.Enabled = true;
                checkBox23.Enabled = true;
            }
            else
            {
                checkBox22.Enabled = false;
                checkBox23.Enabled = false;
                checkBox22.Checked = false;
                checkBox23.Checked = false;
            }
        }

        private void checkBox3_CheckedChanged(object sender, EventArgs e)
        {
          if(  checkBox3.Checked)
            {
                checkBox24.Enabled = true;
                checkBox25.Enabled = true;
            }
            else
            {
                checkBox24.Enabled = false;
                checkBox25.Enabled = false;
                checkBox24.Checked = false;
                checkBox25.Checked = false;
            }
        }

        private void checkBox4_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox4.Checked)
            {
                checkBox26.Enabled = true;
                checkBox27.Enabled = true;
            }
            else
            {
                checkBox26.Enabled =false;
                checkBox27.Enabled = false;
                checkBox26.Checked = false;
                checkBox27.Checked = false;
            }
        }

        private void checkBox5_CheckedChanged(object sender, EventArgs e)
        {
           if( checkBox5.Checked)
            {
                checkBox10.Enabled = true;
                checkBox11.Enabled = true;
            }
           else
            {
                checkBox10.Enabled = false;
                checkBox11.Enabled = false;
                checkBox10.Checked = false;
                checkBox11.Checked = false;
            }
        }

        private void checkBox6_CheckedChanged(object sender, EventArgs e)
        {
           if( checkBox6.Checked)
            {
                checkBox12.Enabled = true;
                checkBox13.Enabled = true;
            }
           else
            {
                checkBox12.Enabled = false;
                checkBox13.Enabled = false;
                checkBox12.Checked = false;
                checkBox13.Checked = false;
            }
        }

        private void checkBox7_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox7.Checked)
            {
                checkBox14.Enabled = true;
                checkBox15.Enabled = true;
            }
            else
            {
                checkBox14.Enabled = false;
                checkBox15.Enabled = false;
                checkBox14.Checked = false;
                checkBox15.Checked = false;
            }
        }

        private void checkBox8_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox8.Checked)
            {
                checkBox16.Enabled = true;
                checkBox17.Enabled = true;
            }
            else
            {
                checkBox16.Enabled = false;
                checkBox17.Enabled = false;
                checkBox16.Checked = false;
                checkBox17.Checked = false;
            }
        }

        private void checkBox9_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox9.Checked)
            {
                checkBox18.Enabled = true;
                checkBox19.Enabled = true;
            }
            else
            {
                checkBox18.Enabled = false;
                checkBox19.Enabled = false;
                checkBox18.Checked = false;
                checkBox19.Checked = false;
            }
        }

        private void panel3_Paint(object sender, PaintEventArgs e)
        {

        }
    }
}
